import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class TextInputToFile {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BufferedWriter writer = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

        try {
            writer = new BufferedWriter(new FileWriter("input.txt", true));

            String input;
            while (true) {
                System.out.println("请输入文字（输入'quit'退出）:");
                input = scanner.nextLine();

                if ("quit".equalsIgnoreCase(input)) {
                    break;
                }

                String currentTime = dateFormat.format(new Date()) + " ";
                writer.write(currentTime + input);
                writer.newLine();
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
                scanner.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
